﻿using System;
using System.Text;
using System.Windows.Forms;

namespace Nathan_tools
{
    public partial class frm_cryptography : Form
    {
        public frm_cryptography()
        {
            InitializeComponent();
        }

        private void frm_cryptography_Load(object sender, EventArgs e)
        {

        }

        private void btn_file_md5_Click(object sender, EventArgs e)
        {
            if (tb_filename.Text == "")
                MessageBox.Show("请输入文件名！","友情提示");
            else
            tb_filecrypt.Text = GetFileMd5Hash(tb_filename.Text);
        }

        public static string GetStrMd5Hash(string str)  // //计算符串md5
        {
            StringBuilder sb = new StringBuilder(); 
            foreach (byte b in System.Security.Cryptography.MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str))) 
             { sb.Append(b.ToString("X2")); }
            return sb.ToString();
        }

        public static string GetFileMd5Hash(string pathName)   //计算文件md5
        {
            string strResult = "";
            string strHashData = "";
            byte[] arrbytHashValue;

            System.IO.FileStream oFileStream = null;
            System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider();

            try
            {
                oFileStream = new System.IO.FileStream(pathName.Replace("\"", ""), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值  
                oFileStream.Close();
                //由以连字符分隔的十六进制对构成的String，其中每一对表示value 中对应的元素；例如“F-2C-4A”  
                strHashData = System.BitConverter.ToString(arrbytHashValue);
                //替换-  
                strHashData = strHashData.Replace("-", "");
                strResult = strHashData;
            }
            catch (System.Exception ex)
            {
                MessageBox .Show (ex.Message);
            }

            return strResult;
        }


        public static string GetStrSHAHash(string str)  // //计算符串md5
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in System.Security.Cryptography.SHA1.Create().ComputeHash(Encoding.UTF8.GetBytes(str)))
            { sb.Append(b.ToString("X2")); }
            return sb.ToString();
        }

        public static string GetFileShaHash(string pathName)
        {
            string strResult = "";
            string strHashData = "";
            byte[] arrbytHashValue;

            System.IO.FileStream oFileStream = null;
            System.Security.Cryptography.SHA1CryptoServiceProvider SHAHasher = new System.Security.Cryptography.SHA1CryptoServiceProvider();

            try
            {
                oFileStream = new System.IO.FileStream(pathName.Replace("\"", ""), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                arrbytHashValue = SHAHasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值  
                oFileStream.Close();
                //由以连字符分隔的十六进制对构成的String，其中每一对表示value 中对应的元素；例如“F-2C-4A”  
                strHashData = System.BitConverter.ToString(arrbytHashValue);
                //替换-  
                strHashData = strHashData.Replace("-", "");

                strResult = strHashData;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            return strResult;
        }



        private void btn_openfile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog1.ShowDialog())
            { tb_filename.Text = openFileDialog1.FileName; }
        }

        private void btn_str_md5_Click(object sender, EventArgs e)
        {
            tb_crypt_str.Text = GetStrMd5Hash(tb_str.Text);
        }

        private void btn_file_hash_Click(object sender, EventArgs e)
        {
            if (tb_filename.Text == "")
                MessageBox.Show("请输入文件名！", "友情提示");
            else
            tb_filecrypt.Text = GetFileShaHash(tb_filename.Text);
        }

        private void btn_str_hash_Click(object sender, EventArgs e)
        {
            tb_crypt_str.Text = GetStrSHAHash(tb_str.Text);
        }
    }
}
